# @Time    :2025/4/7 19:56
import threading
import time


def func():
    global g_num
    for _ in range(10):
        tmp = g_num + 1
        time.sleep(0.3)
        g_num = tmp
        print(f"当前线程{threading.current_thread().name}:{g_num}")


if __name__ == "__main__":
    # 定义一个全局变量
    g_num = 0

    threads = [threading.Thread(target=func, name="线程" + str(i)) for i in range(3)]
    [t.start() for t in threads]
    [t.join() for t in threads]
    print(f"当前线程{threading.current_thread().name}", g_num)



# import threading
# import time
#
#
# def func():
#     global g_num
#     for _ in range(10):
#         tmp = g_num + 1
#         time.sleep(0.3)
#         g_num = tmp
#         print(f"当前线程{threading.current_thread().name}:{g_num}")
#
#
# if __name__ == "__main__":
#     # 定义一个全局变量
#     g_num = 0
#
#     # 创建3个线程对象 这种写法不如列表推导式循环出来 看着简单点
#     # t1=threading.Thread(target=func,)
#     # t2=threading.Thread(target=func,)
#     # t3=threading.Thread(target=func,)
#
#     threads = [threading.Thread(target=func, name="线程" + str(i)) for i in range(3)]
#     [t.start() for t in threads]
#     [t.join() for t in threads]
#     # 上面的列表推导式 等价于下边这种写法
#     # for t in threads:
#     #     t.start()
#     print(f"当前线程{threading.current_thread().name}", g_num)
